package com.jsbj.tools;
import java.util.HashMap;
import java.util.Map;

/**
 * 地球坐标系 (WGS-84) 相互转火星坐标系 (GCJ-02) 的转换算法
 * 
 *
 */
public class WgsToGcj {
	private static double pi = 3.14159265358979324D;// 圆周率
	private static double a = 6378245.0D;// WGS 长轴半径
	private static double ee = 0.00669342162296594323D;// WGS 偏心率的平方
	private static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	/**
	 * 中国坐标内
	 * 
	 * @param lat
	 * @param lon
	 * @return
	 */
	public static boolean outofChina(double lat, double lon) {
		if (lon < 72.004 || lon > 137.8347)
			return true;
		if (lat < 0.8293 || lat > 55.8271)
			return true;
		return false;
	}

	public static double transformLat(double x, double y) {
		double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
				+ 0.2 * Math.sqrt(Math.abs(x));
		ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
		ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
		ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
		return ret;
	}
	// 84->gcj02
	public static double transformLon(double x, double y) {
		double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
				* Math.sqrt(Math.abs(x));
		ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
		ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
		ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
				* pi)) * 2.0 / 3.0;
		return ret;
	}
	/**
	 * gps转火星
	 */
	public static Map<String, Double> transform(double lat,double lon) {
		HashMap<String, Double> localHashMap = new HashMap<String, Double>();
		if (outofChina(lat, lon)) {
			localHashMap.put("lon", Double.valueOf(lon));
			localHashMap.put("lat", Double.valueOf(lat));
			return localHashMap;
		}
		double dLat = transformLat(lon - 105.0, lat - 35.0);
		double dLon = transformLon(lon - 105.0, lat - 35.0);
		double radLat = lat / 180.0 * pi;
		double magic = Math.sin(radLat);
		magic = 1 - ee * magic * magic;
		double sqrtMagic = Math.sqrt(magic);
		dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
		dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
		double mgLat = lat + dLat;
		double mgLon = lon + dLon;
		localHashMap.put("lon", mgLon);
		localHashMap.put("lat", mgLat);
		return localHashMap;
	}
	// gcj02-84
	public static Map<String, Double> gcj2wgs(double lat,double lon) {
		Map<String, Double> localHashMap = new HashMap<String, Double>();
		double lontitude = lon
				- (((Double) transform(lon, lat).get("lon")).doubleValue() - lon);
		double latitude = (lat - (((Double) ( transform(lon, lat))
				.get("lat")).doubleValue() - lat));
		localHashMap.put("lon", lontitude);
		localHashMap.put("lat", latitude);
		return localHashMap;
	}
	
	//-------------------------------------------------------
	
    /**
     * 火星坐标转换为百度坐标
     * @param gg_lat
     * @param gg_lon
     */
	public static Map<String, Double> bd_encrypt(double gg_lat, double gg_lon,double bd_lat,double bd_lon)
    {
		Map<String, Double> localHashMap = new HashMap<String, Double>();
        double x = gg_lon, y = gg_lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        bd_lon = z * Math.cos(theta) + 0.0065;
        bd_lat = z * Math.sin(theta) + 0.006;
        
        
        localHashMap.put("lon", bd_lon);
		localHashMap.put("lat", bd_lat);
        return localHashMap;
    }
	
	
	
	//-------------------------------------------------------
	//gps转火星坐标再转百度坐标
	public static Map<String, Double> GPSByBaidu(double lon,double lat){
		String jd = null;
		String wd = null;
		String jd_q = null;
		String wd_q = null;
		String jd_h = null;
		String wd_h = null;
		jd = ""+lon;
		wd = ""+lat;
		jd = jd.replace(".", ",");
		wd = wd.replace(".", ",");
		String[] jd_shu = jd.split(",");
		String[] wd_shu = wd.split(",");
		Map<String, Double> bd_encrypt = new HashMap<String, Double>();
		if(jd_shu.length == 2 && wd_shu.length == 2){
			jd_q = jd_shu[0];
			wd_q = wd_shu[0];
			jd_h = "0."+jd_shu[1];
			wd_h = "0."+wd_shu[1];
			double jq_q_j = 0.0;
			double wd_q_w = 0.0;
			try {
				jq_q_j = Double.valueOf(jd_q)+(Double.valueOf(jd_h)/60*100);//如果是分或秒需要转换成度(jd_h/60)*100
				wd_q_w = Double.valueOf(wd_q)+(Double.valueOf(wd_h)/60*100);//如果是分或秒需要转换成度(wd_h/60)*100
				Map<String, Double> localHashMap = transform(wd_q_w,jq_q_j);
				
				bd_encrypt = bd_encrypt(localHashMap.get("lat"),localHashMap.get("lon"),0,0);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bd_encrypt;
		
	}
	
	//火星坐标转百度坐标,转化路线图时调用  2016-07-16 mk
	public static Map<String, Double> GPSByBaidu2(double lon,double lat){
		String jd = null;
		String wd = null;
		String jd_q = null;
		String wd_q = null;
		String jd_h = null;
		String wd_h = null;
		jd = ""+lon;
		wd = ""+lat;
		jd = jd.replace(".", ",");
		wd = wd.replace(".", ",");
		String[] jd_shu = jd.split(",");
		String[] wd_shu = wd.split(",");
		Map<String, Double> bd_encrypt = new HashMap<String, Double>();
		if(jd_shu.length == 2 && wd_shu.length == 2){
			jd_q = jd_shu[0];
			wd_q = wd_shu[0];
			jd_h = "0."+jd_shu[1];
			wd_h = "0."+wd_shu[1];
			double jq_q_j = 0.0;
			double wd_q_w = 0.0;
			try {
				jq_q_j = Double.valueOf(jd_q)+(Double.valueOf(jd_h));//如果是分或秒需要转换成度(jd_h/60)*100
				wd_q_w = Double.valueOf(wd_q)+(Double.valueOf(wd_h));//如果是分或秒需要转换成度(wd_h/60)*100
				Map<String, Double> localHashMap = transform(wd_q_w,jq_q_j);
				
				bd_encrypt = bd_encrypt(localHashMap.get("lat"),localHashMap.get("lon"),0,0);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bd_encrypt;
		
	}
	
	// gps 度 转 度分格式
	public static String duByFenFromGps(double zb){
		String zbStr = ""+zb;
		zbStr = zbStr.replace(".", ",");
		String[] zb_shu = zbStr.split(",");
		String fenZb=null;
		if(zb_shu.length == 2){
			String zb_q = zb_shu[0];
			String zb_h = "0."+zb_shu[1];
			Double a = Double.valueOf(zb_h)*60;
			if(a < 10){
				fenZb = zb_q+"0"+(Double.valueOf(zb_h)*60);
			}else{
				fenZb = zb_q+(Double.valueOf(zb_h)*60);
			}
		}
		
		return fenZb;
	}
	
	//如果是分或秒需要转换成度(jd_h/60)*100
	public static Map<String, Double> GPSByDu(double lon,double lat){
		String jd = null;
		String wd = null;
		String jd_q = null;
		String wd_q = null;
		String jd_h = null;
		String wd_h = null;
		jd = ""+lon;
		wd = ""+lat;
		jd = jd.replace(".", ",");
		wd = wd.replace(".", ",");
		String[] jd_shu = jd.split(",");
		String[] wd_shu = wd.split(",");
		Map<String, Double> bd_encrypt = new HashMap<String, Double>();
		if(jd_shu.length == 2 && wd_shu.length == 2){
			jd_q = jd_shu[0];
			wd_q = wd_shu[0];
			jd_h = "0."+jd_shu[1];
			wd_h = "0."+wd_shu[1];
			double jq_q_j = 0.0;
			double wd_q_w = 0.0;
			try {
				jq_q_j = Double.valueOf(jd_q)+(Double.valueOf(jd_h)/60*100);//如果是分或秒需要转换成度(jd_h/60)*100
				wd_q_w = Double.valueOf(wd_q)+(Double.valueOf(wd_h)/60*100);//如果是分或秒需要转换成度(wd_h/60)*100
				bd_encrypt.put("lon", jq_q_j);
				bd_encrypt.put("lat", wd_q_w);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bd_encrypt;
		
	}

	/**
	 * 度分转度
	 * @param lon  11953.79698839
	 * @param lat	3032.32856049
	 * @return
	 */
	public static Map<String, Double> fromDuFenToDu(double lon,double lat){
		String jd = null;
		String wd = null;
		String jd_q = null;
		String wd_q = null;
		String jd_h = null;
		String wd_h = null;
		lon=lon/100.0;
		lat=lat/100.0;
		jd = ""+lon;
		wd = ""+lat;
		jd = jd.replace(".", ",");
		wd = wd.replace(".", ",");
		String[] jd_shu = jd.split(",");
		String[] wd_shu = wd.split(",");
		Map<String, Double> bd_encrypt = new HashMap<String, Double>();
		if(jd_shu.length == 2 && wd_shu.length == 2){
			jd_q = jd_shu[0];
			wd_q = wd_shu[0];
			jd_h = "0."+jd_shu[1];
			wd_h = "0."+wd_shu[1];
			double jq_q_j = 0.0;
			double wd_q_w = 0.0;
			try {
				jq_q_j = Double.valueOf(jd_q)+(Double.valueOf(jd_h)/60*100);//如果是分或秒需要转换成度(jd_h/60)*100
				wd_q_w = Double.valueOf(wd_q)+(Double.valueOf(wd_h)/60*100);//如果是分或秒需要转换成度(wd_h/60)*100
				bd_encrypt.put("lon", jq_q_j);
				bd_encrypt.put("lat", wd_q_w);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return bd_encrypt;

	}

	// 84 转高德
	public static double[] gps84_To_Gcj02(double lat, double lon) {
		double dLat = transformLat(lon - 105.0, lat - 35.0);
		double dLon = transformLon(lon - 105.0, lat - 35.0);
		double radLat = lat / 180.0 * pi;
		double magic = Math.sin(radLat);
		magic = 1 - ee * magic * magic;
		double sqrtMagic = Math.sqrt(magic);
		dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
		dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
		double mgLat = lat + dLat;
		double mgLon = lon + dLon;
		return new double[]{mgLat, mgLon};
	}


}